//-------------------------------------------------------------------------------//

#include "globals.h"
#include "adGame.h"

//-------------------------------------------------------------------------------//

adGame::adGame()
{}

//-------------------------------------------------------------------------------//

adGame::~adGame()
{}

//-------------------------------------------------------------------------------//
// adGame::Setup() - Loads objects, sprites, textures, fonts, etc.
//-------------------------------------------------------------------------------//
void adGame::Setup()
{
    //Initialize Resources
    m_shipBlur = m_rManager->GetParticleSystem ( "shipBlur" );
    m_shipBlur->Fire();

    m_pos = hgeVector ( SCREEN_WIDTH / 2.0f, SCREEN_HEIGHT / 2.0f );
    m_vel = hgeVector ( 0, 0 );
    speed = 30.0f;
    friction = 0.98f;
}

//-------------------------------------------------------------------------------//
// adGame::Cleanup() - Cleans up objects, and frees memory.
//-------------------------------------------------------------------------------//
void adGame::Cleanup()
{}

//-------------------------------------------------------------------------------//
// adGame::Update() - Updates the game, positions, velocities, the world, etc.
//-------------------------------------------------------------------------------//
bool adGame::Update()
{
    // get the delta time, and the mouse position
    float dt = m_HGE->Timer_GetDelta();
    m_HGE->Input_GetMousePos ( &m_mouseX, &m_mouseY );

    //Process Input
    if ( m_HGE->Input_GetKeyState ( HGEK_LEFT ) )
        m_vel.x -= speed * dt;
    if ( m_HGE->Input_GetKeyState ( HGEK_RIGHT ) )
        m_vel.x += speed * dt;
    if ( m_HGE->Input_GetKeyState ( HGEK_UP ) )
        m_vel.y -= speed * dt;
    if ( m_HGE->Input_GetKeyState ( HGEK_DOWN ) )
        m_vel.y += speed * dt;

    // Do some movement calculations and collision detection
    m_vel *= friction;
    m_pos += m_vel;

    // bounce off the walls if we get too close
    if ( m_pos.x > 784 )
    {
        m_pos.x = 784 - ( m_pos.x - 784 );
        m_vel.x = -m_vel.x;
    }
    if ( m_pos.x < 16 )
    {
        m_pos.x = 16 + 16 - m_pos.x;
        m_vel.x = -m_vel.x;
    }
    if ( m_pos.y > 584 )
    {
        m_pos.y = 584 - ( m_pos.y - 584 );
        m_vel.y = -m_vel.y;
    }
    if ( m_pos.y < 16 )
    {
        m_pos.y = 16 + 16 - m_pos.y;
        m_vel.y = -m_vel.y;
    }

    m_shipBlur->info.nEmission = 25;
    m_shipBlur->MoveTo ( m_pos.x, m_pos.y );
    m_shipBlur->Update ( dt );

    return m_HGE->Input_GetKeyState ( HGEK_ESCAPE );
}

//-------------------------------------------------------------------------------//
// adGame::Render() - Renders the game, texture, fonts, the world, etc.
//-------------------------------------------------------------------------------//
void adGame::Render()
{
    m_HGE->Gfx_BeginScene();
    m_HGE->Gfx_Clear ( 0xFF323232 );
    {
        // render the fps counter in the top left corner
        m_rManager->GetFont ( "smallFont" )->printf ( 5, 5, HGETEXT_LEFT, "%d FPS\neM: %.3f", m_HGE->Timer_GetFPS(), m_shipBlur->info.nEmission );

        m_shipBlur->Render();
        m_rManager->GetSprite ( "playerShip" )->Render ( m_pos.x, m_pos.y );
    }

    m_HGE->Gfx_EndScene();
}

//-------------------------------------------------------------------------------//
